package com.lxq.interceptor;

import com.lxq.model.Transaction;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class TransactionValidator  implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {

        return Transaction.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Transaction trans= (Transaction) target;
        //计算金额差价
        double th=trans.getAmount()-(trans.getPrice()*trans.getQuantity());
        if (Math.abs(th)>0.01){
            errors.rejectValue("amount",null,"金额不符");
        }
    }
}
